<?php
$title = "Rank Checker";
require_once("header.php");
require_once("functions.php");

$temp_pageurls = array();
$temp_keywords = '';

if (isset($_REQUEST['pageurls']))
	$temp_pageurls = $_REQUEST['pageurls'];

if (isset($_REQUEST['keywords']))
	$temp_keywords = $_REQUEST['keywords'];

//echo "<pre>"; print_r($_REQUEST); die;

$num = intval(@$_REQUEST['num']);

$limit = array(100,200,300,400,500,600,700,800,900,1000);
?>
<script src="javascript.js" language="Javascript"></script>
<script type="text/javascript">
var pageurls = '<?php if(is_array($temp_pageurls)) echo addslashes(implode('~@~', $temp_pageurls)); else echo addslashes($temp_pageurls); ?>';
var keywords = '<?php if(is_array($temp_keywords)) echo addslashes(implode('~@~', $temp_keywords)); else echo addslashes($temp_keywords); ?>';
var num = <?php echo $num; ?>;
var max_time_allowed = 600;

var t2;
var t1;
var t3;

var start_yahoo_req = 0;
var total_yahoo_req = 1;
var yahoo_results = '';

var start_msn_req = 0;
var total_msn_req = 0;
var msn_results = '';

var start_google_req = 0;
var total_google_req = 0;
var google_results = '';

function startTime1()
{
	dataObj2 = new Date();
	t = dataObj2.getTime();
	t = (t - mytime1)/(1000);
	if (t > max_time_allowed)
	{
		try
		{
			start_msn_req = 11;
			document.getElementById('msn_process').innerHTML = '';
			clearTimeout(t1);
		}
		catch (e) {}
	}
	else
	{
		document.getElementById('txt1').innerHTML = ''+Math.floor(t/60)+':'+Math.round(t%60)+' Mins';
		t1 = setTimeout('startTime1()',500)
	}
}
function startTime2()
{
	dataObj2 = new Date();
	t = dataObj2.getTime();
	t = (t - mytime2)/(1000);
	if (t > max_time_allowed)
	{
		try
		{
			start_yahoo_req = 11;
			document.getElementById('yahoo_progress').innerHTML = '';
			clearTimeout(t2);
		}
		catch (e) {}
	}
	else
	{
		document.getElementById('txt2').innerHTML = ''+Math.floor(t/60)+':'+Math.round(t%60)+' Mins';
		t2 = setTimeout('startTime2()',500);
	}
}
function startTime3()
{
	dataObj2 = new Date();
	t = dataObj2.getTime();
	t = (t - mytime3)/(1000);
	if (t > max_time_allowed)
	{
		try
		{
			start_google_req = 11;
			document.getElementById('google_progress').innerHTML = '';
			clearTimeout(t3);
		}
		catch (e) {}
	}
	else
	{
		document.getElementById('txt3').innerHTML = ''+Math.floor(t/60)+':'+Math.round(t%60)+' Mins';
		t3 = setTimeout('startTime3()',500);
	}
}

function do_forgoogle(ob) {
	//alert("GOOGLE"+ob.responseText);
	start_google_req++;
	
	progress_bar(total_google_req, start_google_req,'google_progress');

	if(ob.responseText != '') {
		try{
			display_text('google', ob.responseText);
		} catch(e) {
		}
	}
	if (start_google_req >= total_google_req)
	{
		if (google_results != '')
		{
			try
			{
				document.getElementById('google_process').innerHTML = '';
				clearTimeout(t3);
			}
			catch (e) {}
		}
		else
		{
			try
			{
				document.getElementById('google').innerHTML = '<h3>No Results Found</h3>';
				clearTimeout(t3);
			}
			catch (e) {}
		}
	}
	else
	{
		get_google(start_google_req+1);
	}
}

function get_google(n) {
	xmlhttp_config = {"URL":"query_google.php","METHOD":"POST","ASYNCH":true};
	xmlHRH = new xmlHttpRequestHandler(xmlhttp_config);
	xmlHRH.setData("pageurls="+pageurls+"&keywords="+keywords+"&num="+n);
	xmlHRH.send(do_forgoogle);
	
}

function do_formsn(ob) {
	//alert("MSN");
	start_msn_req++;
	
	progress_bar(total_msn_req, start_msn_req,'msn_progress');

	if(ob.responseText != '') {
		try{
			display_text('msn', ob.responseText);
		} catch(e) {
		}
	}
	if (start_msn_req >= total_msn_req)
	{
		if (msn_results != '')
		{
			try
			{
				document.getElementById('msn_process').innerHTML = '';
				clearTimeout(t1);
			}
			catch (e) {}
		}
		else
		{
			try
			{
				document.getElementById('msn').innerHTML = '<h3>No Results Found</h3>';
				clearTimeout(t1);
			}
			catch (e) {}
		}
	}
	else
	{
		get_msn(start_msn_req+1);
	}
}

function get_msn(n) {
	xmlhttp_config = {"URL":"query_msn.php","METHOD":"POST","ASYNCH":true};
	xmlHRH = new xmlHttpRequestHandler(xmlhttp_config);
	xmlHRH.setData("pageurls="+pageurls+"&keywords="+keywords+"&num="+n);
	xmlHRH.send(do_formsn);
	
}

function do_foryahoo(ob) {
	//alert("Yahoo!");
	start_yahoo_req++;

	progress_bar(total_yahoo_req, start_yahoo_req,'yahoo_progress');

	if(ob.responseText != '') {
		try{
			display_text('yahoo', ob.responseText);
		} catch(e) {
		}
	}

	if (start_yahoo_req >= total_yahoo_req)
	{
		if (yahoo_results != '')
		{
			try
			{
				document.getElementById('yahoo_process').innerHTML = '';
				clearTimeout(t2);
			}
			catch (e) {}
		}
		else
		{
			try
			{
				document.getElementById('yahoo').innerHTML = '<h3>No Results Found</h3>';
				clearTimeout(t2);
			}
			catch (e) {}
		}
	}
	else
	{
		get_yahoo(start_yahoo_req+1);
	}
}

function get_yahoo(n) {
	xmlhttp_config = {"URL":"query_yahoo.php","METHOD":"POST","ASYNCH":true};
	xmlHRH = new xmlHttpRequestHandler(xmlhttp_config);
	xmlHRH.setData("pageurls="+pageurls+"&keywords="+keywords+"&num="+n);
	xmlHRH.send(do_foryahoo);
	
}

function display_text(divs, response_text) {

	var numchars = 16;

	if (divs == 'msn')
	{
		msn_results = 'found';
	}
	if (divs == 'yahoo')
	{
		yahoo_results = 'found';
		numchars = 17;
	}
	if (divs == 'google')
	{
		google_results = 'found';
	}
	str = document.getElementById(divs).innerHTML;
	fstr = str.substring(0,str.length-numchars);
	document.getElementById(divs).innerHTML = fstr + response_text + ' </tbody></table>';
}

</script>
<?php

if (!empty($temp_pageurls) && !empty($temp_keywords) && isset($_REQUEST['submit']) ) {
//#########################################################################################
//		GOOGLE
//#########################################################################################
if ( isset($_REQUEST['do_google']) )
	{
		$t = 100;
		echo '<script>';
		echo 'total_google_req = '.($num/100).";\n";
/*
		for ($i=1; $i<=$num/100; $i++ )
		{
			echo 'setTimeout("get_google('.$i.')",'.$t.');'."\n";
			$t += 1000;
		}
		*/
		echo 'setTimeout("get_google(1)",'.$t.');'."\n";
		echo '</script>';

?>
<h1>Google</h1>
<div id="google">
<div id="google_process" class="fontv10pxb646464">
<table width="98%" border="0" cellspacing="2" cellpadding="2" class="fontv10pxb646464">
<tr><td width="60%">Processing. It may take few minuts. Please wait..........</td>
<td align="center"><?php echo progress_bar($num/100,0,'google_progress'); ?></td>
<td><div id="txt3"></div>
<script type="text/javascript">
var dataObj = new Date();
var mytime3 = dataObj.getTime();
startTime3();
</script></td></tr></table>
</div>
<table width="98%" border="0" cellspacing="2" cellpadding="2">
<tbody>
  <tr class="tr1" height="25">
    <td width="15%" align="left"><B>Page Title</B></td>
	<td width="15%" align="left"><B>Keyword</B></td>
    <td width="5%" align="center"><B>Rank</B></td>
	<td align="left"><B>Full Url</B></td>
  </tr>
 </tbody></table>
 </div><p>&nbsp;</p>
<?php
	}
//#########################################################################################
//		MSN
//#########################################################################################
	if ( isset($_REQUEST['do_msn']) )
	{
		$t = 50;
		echo '<script>';
		echo 'total_msn_req = '.($num/100).";\n";
/*
		for ($i=1; $i<=$num/100; $i++ )
		{
			echo 'setTimeout("get_msn('.$i.')",'.$t.');'."\n";
			$t += 1000;
		}
		*/
		echo 'setTimeout("get_msn(1)",'.$t.');'."\n";
		echo '</script>';

?>
<h1>MSN</h1>
<div id="msn">
<div id="msn_process" class="bodytext">
<table width="98%" border="0" cellspacing="2" cellpadding="2" class="fontv10pxb646464">
<tr><td width="60%">Processing. It may take few minuts. Please wait..........</td>
<td align="center"><?php echo progress_bar($num/100,0,'msn_progress'); ?></td>
<td><div id="txt1"></div>
<script type="text/javascript">
var dataObj = new Date();
var mytime1 = dataObj.getTime();
startTime1();
</script></td></tr></table>
</div>
<table width="98%" border="0" cellspacing="2" cellpadding="2">
<tbody>
  <tr class="tr1" height="25">
    <td width="15%" align="left"><B>Page Title</B></td>
	<td width="15%" align="left"><B>Keyword</B></td>
    <td width="5%" align="center"><B>Rank</B></td>
	<td align="left"><B>Full Url</B></td>
  </tr>
 </tbody></table>
 </div><p>&nbsp;</p>
<?php
	}
//#########################################################################################
//		YAHOO
//#########################################################################################
	if ( isset($_REQUEST['do_yahoo']) )
	{
		$t = 80;
		echo '<script>';
		echo 'total_yahoo_req = '.($num/100).";\n";
/*
		for ($i=1; $i<=$num/100; $i++ )
		{
			echo 'setTimeout("get_yahoo('.$i.')",'.$t.');'."\n";
			$t += 1000;
		}
		*/
		echo 'setTimeout("get_yahoo(1)",'.$t.');'."\n";
		echo '</script>';
?>
<h1>Yahoo!</h1>
<div id="yahoo">
<div id="yahoo_process" class="bodytext">
<table width="98%" border="0" cellspacing="2" cellpadding="2" class="fontv10pxb646464">
<tr><td width="60%">Processing. It may take few minuts. Please wait..........</td>
<td align="center"><?php echo progress_bar($num/100,0,'yahoo_progress'); ?></td>
<td>
<div id="txt2"></div>
<script type="text/javascript">
var dataObj = new Date();
var mytime2 = dataObj.getTime();
startTime2();
</script>
</td></tr></table>
</div>
<table width="98%" border="0" cellspacing="2" cellpadding="2">
<tbody>
  <tr class="tr1" height="25">
    <td width="15%" align="left"><B>Page Title</B></td>
	<td width="15%" align="left"><B>Keyword</B></td>
    <td width="5%" align="center"><B>Rank</B></td>
	<td align="left"><B>Full Url</B></td>
  </tr>
 </tbody></table>
</div><p>&nbsp;</p>
<?php
	}
}

require_once("forms.php");

require_once("footer.php");
?>